/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Decorator } from './Decorator.js'
import { Random } from './Random.js'
import { Selector } from './Selector.js'
import { Sequence } from './Sequence.js'
import { Task } from './Task.js'
import { AlwaysFailDecorator } from './decorators/AlwaysFailDecorator.js'
import { AlwaysSucceedDecorator } from './decorators/AlwaysSucceedDecorator.js'
import { CooldownDecorator } from './decorators/CooldownDecorator.js'
import { InvertDecorator } from './decorators/InvertDecorator.js'
import { LoopDecorator } from './decorators/LoopDecorator.js'
import { BehaviorTree,registryLookUp } from './BehaviorTree.js'

"use strict";

var __importDefault = this && this.__importDefault || function (mod) {
    return mod && mod.__esModule ? mod : { "default": mod };
};

const Decorator_1 = __importDefault(Decorator);
const Random_1 = __importDefault(Random);
const Selector_1 = __importDefault(Selector);
const Sequence_1 = __importDefault(Sequence);
const Task_1 = __importDefault(Task);
const AlwaysFailDecorator_1 = __importDefault(AlwaysFailDecorator);
const AlwaysSucceedDecorator_1 = __importDefault(AlwaysSucceedDecorator);
const CooldownDecorator_1 = __importDefault(CooldownDecorator);
const InvertDecorator_1 = __importDefault(InvertDecorator);
const LoopDecorator_1 = __importDefault(LoopDecorator);
const BehaviorTree_1 = BehaviorTree;
class BehaviorTreeImporter {
    constructor() {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        this.types = {
            task: Task_1.default,
            decorator: Decorator_1.default,
            selector: Selector_1.default,
            sequence: Sequence_1.default,
            random: Random_1.default,
            invert: InvertDecorator_1.default,
            fail: AlwaysFailDecorator_1.default,
            succeed: AlwaysSucceedDecorator_1.default,
            cooldown: CooldownDecorator_1.default,
            loop: LoopDecorator_1.default
        };
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    defineType(type, Klass) {
        this.types[type] = Klass;
    }
    parse(json) {
        //const { type, name, ...config } = json;
        const { type, name, config } = json;
        const Klass = this.types[type];
        if (!Klass) {
            const registeredNode = (0, registryLookUp)(type);
            if (registeredNode) {
                registeredNode.name = name;
                return registeredNode;
            }
            throw new Error(`Don't know how to handle type ${type}. Please register this first.`);
        }
        return new Klass({
            name: name,
            node: json.node ? this.parse(json.node) : null,
            nodes: json.nodes ? json.nodes.map(subJson => this.parse(subJson)) : null,
            config
        });
    }
}
// exports.default = BehaviorTreeImporter;
export { BehaviorTreeImporter }